//
//  CCDBaseTableView.m
//  crowd
//
//  Created by 马腾飞 on 16/4/8.
//  Copyright © 2016年 caocaod.com. All rights reserved.
//

#import "CCDBaseTableView.h"
#import "MJRefresh.h"
@interface CCDBaseTableView()
@property (strong, nonatomic)               NSString *aesString;

@end

@implementation CCDBaseTableView
- (void)addMJRefreshHeader:(SEL)downAction {
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:downAction];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.mj_header = header;
}

- (void)addMJRefreshFooter:(SEL)upAction {
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:upAction];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    self.mj_footer = footer;
}

#pragma createAesKey
- (void)createAesKey {
    WS(weakSelf);
    NSString *aesKey = [NSString getAuthcode];
    if(UserValue(@"AESKey")) {
        self.aesString = UserValue(@"AESKey");
    }
    UserSetValueForKey(aesKey, @"ASEKey");
    NSLog(@"%@",aesKey);
    NSString *timeStr = [NSString getCurrentTime];
    NSDictionary *aesDic = @{
                             @"key":aesKey,
                             @"time":timeStr
                             };
    
    NSString *aesString = [NSString getJsonStringWithDic:aesDic];
    NSString *encString = [RSAUtil encryptString:aesString publicKey:UserValue(@"rsa_public_key")];
    
    NSString *baseUrl = @"http://serv.caocaod.com/app/main/createAesKey";
    NSDictionary *dict = UserValue(@"rsa_app_ver");
    NSDictionary *newDic = @{
                             @"verRsa":dict[@"verRsa"],
                             @"verApp":[NSString getAppVer],
                             @"app":@"com.caocaod.work",
                             @"platform":@"iOS",
                             @"rsa":encString
                             };
    
    [self postWithUrl:baseUrl params:newDic progressView:nil successBlock:^(id JSON) {
        NSDictionary *tempDic = JSON;
        if([tempDic[@"result"] integerValue] == 1) {
            NSString *string = tempDic[@"aes"];
            NSString *descString1 = [AESSecurityTool decryptAndBase64ToStringWithString:string];
            NSDictionary *cookieDic = [NSDictionary dictionaryWithJsonString:descString1];
            if(1 == [cookieDic[@"result"] integerValue]) {
                //如果结果为1.则保存AESKEY
                if(UserValue(@"AESKey")) {
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AESKey"];
                }
                UserSetValueForKey(aesKey, @"AESKey");
                
                if(UserValue(@"cookie")) {
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cookie"];
                }
                UserSetValueForKey(cookieDic[@"cookie"], @"cookie");
            }
            NSLog(@"%@",tempDic);
            //如果成功
        }else {
            if(weakSelf.allIndex <= 2) {
                weakSelf.allIndex ++;
                [weakSelf createAesKey];
            } else {
                [weakSelf makeToast:@"服务器异常，请稍候再试" duration:1.5 position:@"center"];
                weakSelf.allIndex = 0;
            }
            if(self.aesString) {
                UserSetValueForKey(self.aesString, @"AESKey");
            }
        }
    } faithBlock:^(NSError *error) {
        [weakSelf makeToast:@"请检查您的网络" duration:1.5 position:@"center"];

        NSLog(@"error:%@",error);
    }];
}


- (void)postWithUrl:(NSString *)url params:(NSDictionary *)params progressView:(UIProgressView *)progressView successBlock:(SuccessBlock)httpSuccess faithBlock:(FaithBlock)httpFaith {
    [CCDHttpBaseTool postWithPath:url params:params progress:nil success:^(id JSON) {
        httpSuccess(JSON);
    } failure:^(NSError *error) {
        httpFaith(error);
    }];
}

@end
